﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using FineUI;
using FTchina.Library;
using FTchina.Logic;

public partial class ui_zdsyx_Default : BasePage
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            if (LoginLogic.RobotLogin())
            {//自动执行                 
                LogHelper.Info("自动执行触发");
                Response.Write(Dosend());
            }
            Grid1.DataSource = GetData();
            Grid1.DataBind();
        }
    }


    protected DataSet GetData()
    {
        PWMIS.DataProvider.Data.AdoHelper ado = PWMIS.DataProvider.Adapter.MyDB.GetDBHelperByConnectionName("DYDB");

        string sql = @"SELECT *
FROM 
(SELECT x.DEPART_NAME,x.MAN,x.TEL,X.EMAIL, COUNT(x.DEPART_ID) AS UCOUNT ,COUNT(x.DEPART_ID)*4000 AS okcount  FROM ZDSYX_LIST  x
WHERE x.UDID IS NOT NULL  
GROUP BY x.DEPART_NAME,x.MAN,x.TEL,x.EMAIL) h LEFT JOIN

(SELECT t.DEPART_NAME,nvl(SUM( t.HIT),0) AS hits from zdsyx_hit_count t GROUP BY t.DEPART_NAME) m 
ON h.DEPART_NAME=m.DEPART_NAME";
        return ado.ExecuteDataSet(sql);
    }
    protected void btnSend_Click(object sender, EventArgs e)
    {

        Alert.ShowInTop(Dosend());
    }


    /// <summary>
    /// 发送
    /// </summary>
    /// <returns></returns>
    protected string Dosend()
    {
        string ErrorMessage = string.Empty;
        string tmp = string.Empty;
        string message = "本月：{0}配备IPAD{1}台，应完成点击数{2}，实际完成{3}。请及时调度。";
        DataSet ds = GetData();
        foreach (DataRow dr in ds.Tables[0].Rows)
        {
            string hits = dr["HITS"].ToString().Trim().Length==0 ? "0" : dr["HITS"].ToString();
            string content = string.Format(message, dr["DEPART_NAME"], dr["UCOUNT"], dr["OKCOUNT"], hits);
            FTchina.OA.UnicomSMS.SendSMS(dr["TEL"].ToString(), "走动式营销系统", content, out tmp);
            FTchina.OA.Email.Send(dr["EMAIL"].ToString(), "走动式营销考核日报", content);
            ErrorMessage += tmp;
        }
        return ErrorMessage;
    }


}